package com.chixing.controller;

import com.chixing.util.DateUtil;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Controller
public class SendMessageController {
    @Autowired
    RabbitTemplate rabbitTemplate;
    @GetMapping("sendDirectMessage")
    @ResponseBody
    public String sendDirectMessge(){
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "test message, hello!";
        String crateTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String,Object> map = new HashMap<>();
        map.put("messageId",messageId);
        map.put("messageData",messageData);
        map.put("crateTime",crateTime);
        // 将消息携带绑定键值：TestDirectRouting 发送到交换机TestDirectExchange
        rabbitTemplate.convertAndSend("TestDirectExchange","TestDirectRouting",map);
        return "ok";
    }
    @GetMapping("sendTopicMesage1")
    @ResponseBody
    public String sendTopicMessage1(){
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "message: M A N";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String,Object> manMap = new HashMap<>();
        manMap.put("messageId",messageId);
        manMap.put("messageData",messageData);
        manMap.put("createTime",createTime);
        rabbitTemplate.convertAndSend("topicExchange","topic.man",manMap);
        return "ok";
    }
    @GetMapping("sendTopicMesage2")
    @ResponseBody
    public String sendTopicMessage2(){
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "message: woman is all";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String,Object> manMap = new HashMap<>();
        manMap.put("messageId",messageId);
        manMap.put("messageData",messageData);
        manMap.put("createTime",createTime);
        rabbitTemplate.convertAndSend("topicExchange","topic.woman",manMap);
        return "ok";
    }
    @GetMapping("sendFanoutMessage")
    @ResponseBody
    public String sendFanoutMessage(){
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "message: testFanoutMessage ";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String,Object> map = new HashMap<>();
        map.put("messageId",messageId);
        map.put("messageData",messageData);
        map.put("createTime",createTime);
        rabbitTemplate.convertAndSend("fanoutExchange",null,map);
        return "ok";
    }
/*  测试消息推送 找不到交换机*/
    @GetMapping("/TestMessageAck")
    @ResponseBody
    public String TestMessageAck() {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "message: non-existent-exchange test message ";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        map.put("createTime", createTime);
        rabbitTemplate.convertAndSend("non-existent-exchange", "TestDirectRouting", map);
        return "ok";
    }

    @GetMapping("/TestMessageAck2")
    @ResponseBody
    public String TestMessageAck2() {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "message: lonelyDirectExchange test message ";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        map.put("createTime", createTime);
        rabbitTemplate.convertAndSend("lonelyDirectExchange", "TestDirectRouting", map);
        return "ok";
    }
    @GetMapping("/TestDeadLetter")
    @ResponseBody
    public String TestDeadLetter(){
//        rabbitTemplate.convertAndSend("businessExchange","emp.add","hello");
        System.out.println("发送时间："+DateUtil.dateToString(new Date(),DateUtil.DATE_PATTEN_HMS));
        rabbitTemplate.convertAndSend("exchange","Q1","hello");
        return "ok";
    }
    /**
     * 设置消息有效期时间
     *
     * @param msg
     */
    public void sendDelayMsg(String msg, Long delayTime) {
        System.out.println("cache-queue 发送时间:" + System.currentTimeMillis() + ",msg:" + msg + ",过期时间：" + delayTime);

        MessagePostProcessor postProcessor = new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                message.getMessageProperties().setExpiration(delayTime + "");
                return message;
            }
        };
        rabbitTemplate.convertAndSend("Q1", (Object) msg, postProcessor);
    }
}
